/**
 * @author Created by qiush on 2017-05-31.
 */

import * as React from 'react';

import store from '../../../store';
import {ActionType} from '../../../store/ActionType';
import {IAttrRendererProps} from './IAttrRendererProps';

interface IObj {
  [name: string]: any;
}

class AbsAttrRenderer<S> extends React.Component<IAttrRendererProps, S> {

  protected notifyViewChanged(newValue: any) {

    const {designId, attribute, selectedView, target} = this.props;

    if (target === 'attrVals') {
      const viewAttrs: IObj = {};
      viewAttrs[attribute.name] = newValue;

      store.dispatch({
        type: ActionType.UPDATE_VIEW_ATTR,
        designId,
        viewId: selectedView.id,
        viewAttrs
      });

    } else if (target === 'style') {
      const style: IObj = {};
      style[attribute.name] = newValue;

      store.dispatch({
        type: ActionType.UPDATE_VIEW_STYLE,
        designId,
        viewId: selectedView.id,
        style
      });

    } else if (target === 'hoverStyle') {
      const style: IObj = {};
      style[attribute.name] = newValue;

      store.dispatch({
        type: ActionType.UPDATE_VIEW_HOVER_STYLE,
        designId,
        viewId: selectedView.id,
        style
      });
    }
  }
}

export default AbsAttrRenderer;
